import axios from "axios";
import { getToken } from "./token";

const service = axios.create({
    baseURL: '/app/',
    timeout: 20000
})

//请求拦截器
service.interceptors.request.use(config => {
    //获取token,从Pinia里面获取
    const token = getToken()
    if(token) {
        (config.headers)['token'] = token
    }
    return config
})

//响应拦截器
service.interceptors.response.use(
    res => {
        let message = ''
        if(res.data.code !== 200) {
            switch(res.data.code) {
                case 501:
                    message = '用户名错误';
                    break
                case 503:
                    message = '用户密码错误';
                    break
                case 504:
                    message = '登录信息已过期';
                    break
                case 505:
                    message = '用户名已占用';
                    break
                case 551:
                    message = '公司未找到';
                    break
                default:
                    message = '其他错误'
            }
            return Promise.reject(new Error(message))
        }else {
            return res.data.data
        }
    }
)

export default service;